<?php

namespace App\Http\Controllers\Recharge;

use App\Http\Controllers\Basic\Order\BasicPay;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Server\RechargeController;
use App\Models\Calls;
use App\Models\Flow;
use App\Models\MobileArea;
use App\Models\RechargeOrder;
use App\Models\User;
use function base64_decode;
use EasyWeChat\Factory;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use function json_decode;
use function print_r;
use Request;
use App\Library\MgrResponse;
use function setcookie;
use function sleep;
use function strlen;
use function strpos;
use function substr;
use Validator;
use Exception;
use function var_dump;
use function view;
use Cookie;
use Session;

class AuthController extends Controller
{

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * 获取后台请求参数
     * @param $rule
     * @return mixed|string
     */
    public function oauth() {
        \Log::info('oauth 获取后台请求参数');
        \Log::info($_REQUEST);
        $config = [
            'app_id' => config('wechat.appid'),
            'secret' => config('wechat.appsecret'),

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'oauth' => [
                'scopes'   => ['snsapi_base'],
                'callback' => '/oauth_callback',
            ],
        ];

        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;



        // 获取 OAuth 授权结果用户信息
        $user = $oauth->user()->toArray();
        \Log::info("useruseruseruseruseruseruseruseruseruseruseruseruseruser");
        \Log::info($user);

        //Session::put('open_id', $user['id']);
        //print_r($user->toArray());
        //
        //var_dump(session('target_url'));
        $nickname = '';
        $headimgurl = '';

        try {

            $m = new User;
            $m->open_id = $user['id'];

            if (isset($user['original']['nickname'])) {
                $m->nickname = $user['original']['nickname'];
                $nickname = $user['original']['nickname'];
            }
            if (isset($user['original']['headimgurl'])) {
                $m->headimgurl = $user['original']['headimgurl'];
                $headimgurl = $user['original']['headimgurl'];
            }
            //$m->subscribe = $user['subscribe'];
            $m->save();
        } catch (Exception $e) {
            \Log::info($e->getMessage());
        }

        $targetUrl = base64_decode(Request::input('target_url', null));
        $targetUrl = $targetUrl ? $targetUrl : '/';
        \Log::info('$targetUrl:' . $targetUrl);

        if (strpos($targetUrl, 'dz.ddjuly.com') !== false) {
            $targetUrl .= "&open_id={$user['id']}&nickname={$nickname}&headimgurl={$headimgurl}";
        }

        \Log::info($targetUrl);

        return response()->view('oauth', [
            'targetUrl' => $targetUrl,
        ])->cookie('open_id', $user['id'], 99999999);
    }


}
